//
//  PKRegisterViewController.swift
//  Panku
//
//  Created by mac on 2017/2/10.
//  Copyright © 2017年 Monster. All rights reserved.
//

import UIKit
import YYKit
import SnapKit
import ReactiveCocoa
import ReactiveSwift
import Result

import SVProgressHUD

class PKRegisterViewController: PKBaseViewController {

    //MARK: - init
    init(viewModel:PKRegisterViewModel){
        super.init(nibName: nil, bundle: nil)
        
        self.viewModel = viewModel
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - properties
    //viewModel
    var viewModel:PKRegisterViewModel?
    
    //UI
    lazy var avatarView = UIImageView()
    lazy var backgroundView = UIImageView()
    lazy var cellNumberTextField = PKTextField()
    lazy var verificationNumberField = PKTextField()
    lazy var passwordTextfield = PKTextField()
    lazy var registerButton = PKBaseButton.createButton()
    lazy var thirdLoginLabel = UILabel()
    
    lazy var wechatButton = PKBaseButton.createButton()
    lazy var qqButton = PKBaseButton.createButton()
    lazy var weiboButton = PKBaseButton.createButton()

    
    //MARK: - life circle
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }
    
    //MARK: - setupUI
    override func setupRootView() {
        view.backgroundColor = UIColor.white
        
        let tapGesture = UITapGestureRecognizer { (_) in
            self.view.endEditing(true)
        }
        
        view.addGestureRecognizer(tapGesture)
        
        title = "注册"
    }
    
    override func setupSubViews() {
        view.addSubview(avatarView)
        view.addSubview(backgroundView)
        view.addSubview(cellNumberTextField)
        view.addSubview(verificationNumberField)
        view.addSubview(passwordTextfield)
        view.addSubview(registerButton)
        view.addSubview(thirdLoginLabel)
        view.addSubview(wechatButton)
        view.addSubview(qqButton)
        view.addSubview(weiboButton)

        avatarView.image = R.image.login_avatar()
        backgroundView.image = R.image.login_background()
        

        cellNumberTextField.textColor = UIColor.black
        cellNumberTextField.dsl_textField_placeholderColor = UIColor.lightGray
        cellNumberTextField.placeholder = "请输入自己的手机号"
        cellNumberTextField.seperationView.backgroundColor = UIColor(hexString: "B9B9B9")
        
        verificationNumberField.placeholder = "请输入验证码"
        let getVerificationButton = PKBaseButton.createButton()
        getVerificationButton.setTitle("获取验证码", for: .normal)
        getVerificationButton.frame = CGRect(x: 0, y: 0, width: 100, height: 44)
        getVerificationButton.addBlock(for: .touchUpInside) { (_) in
            //TODO:发送验证码
        }
        
        verificationNumberField.textColor = UIColor.black
        verificationNumberField.seperationView.backgroundColor = UIColor(hexString: "B9B9B9")
        verificationNumberField.dsl_textField_placeholderColor = UIColor.lightGray
        verificationNumberField.rightView = getVerificationButton

        passwordTextfield.textColor = UIColor.black
        passwordTextfield.dsl_textField_placeholderColor = UIColor.lightGray
        passwordTextfield.seperationView.backgroundColor = UIColor(hexString: "B9B9B9")
        passwordTextfield.placeholder = "请输入密码"
        
        
        registerButton.setTitle("注册", for: .normal)
        registerButton.setTitleColor(UIColor.white, for: .normal)
        registerButton.backgroundColor = UIColor.black
        registerButton.layer.cornerRadius = 4
        registerButton.layer.masksToBounds = true
        
        registerButton.addBlock(for: .touchUpInside) { ( button) in
            let accessId = self.cellNumberTextField.text ?? ""
            let accessType = "FK"
            let passwd = self.passwordTextfield.text ?? ""
            
            //hud
            SVProgressHUD.show()
            
            PKLoginServices.register(accessId: accessId, accessType: accessType, passwd: passwd).startWithResult({ (result) in
                
                //dismiss hud
                SVProgressHUD.dismiss()
                
                if result.error == nil{
                    let dict = result.value as! NSDictionary
                    let ok = dict["ok"] as! Bool
                    
                    if ok == false{
                        let errorMessage = dict["msg"] as! String
                        SVProgressHUD.showError(withStatus: "注册失败" + " " + errorMessage)
                        return
                    }else{
                        
                        self.dismiss(animated: true, completion: {
                            
                        })
                    }
                    
                }else{
                    SVProgressHUD.showError(withStatus: "注册失败" + result.error!.localizedDescription)
                }
            })
        }
        
        thirdLoginLabel.text = "第三方登录"
        thirdLoginLabel.font = UIFont.PKFont(Size: 13)
        thirdLoginLabel.textColor = UIColor(hexString: "4A4A4A")
        
        weiboButton.setImage(R.image.login_weibo(), for: .normal)
        wechatButton.setImage(R.image.login_wechat(), for: .normal)
        qqButton.setImage(R.image.login_qq(), for: .normal)
        
    }
    
    override func setupSubViewsConstraints() {
        
        avatarView.snp.makeConstraints { (make) in
            make.top.equalTo(view.snp.top).offset(26)
            make.centerX.equalTo(view.snp.centerX)
        }
        
        backgroundView.snp.makeConstraints { (make) in
            make.left.equalTo(view.snp.left)
            make.right.equalTo(view.snp.right)
            make.bottom.equalTo(view.snp.bottom)
        }
        
        cellNumberTextField.snp.makeConstraints { (make) in
            make.left.equalTo(view.snp.left).offset(63*D_FACTOR_X)
            make.right.equalTo(view.snp.right).offset(-63*D_FACTOR_X)
            make.top.equalTo(avatarView.snp.bottom).offset(24*D_FACTOR_Y)
            make.height.equalTo(44)
        }
        
        verificationNumberField.snp.makeConstraints { (make) in
            make.left.equalTo(cellNumberTextField.snp.left)
            make.right.equalTo(cellNumberTextField.snp.right)
            make.top.equalTo(cellNumberTextField.snp.bottom).offset(35*D_FACTOR_Y)
            make.height.equalTo(44)
        }
        
        passwordTextfield.snp.makeConstraints { (make) in
            make.left.equalTo(cellNumberTextField.snp.left)
            make.right.equalTo(cellNumberTextField.snp.right)
            make.top.equalTo(verificationNumberField.snp.bottom).offset(35*D_FACTOR_Y)
            make.height.equalTo(44)
        }
        
        
        registerButton.snp.makeConstraints { (make) in
            make.left.equalTo(cellNumberTextField.snp.left)
            make.right.equalTo(cellNumberTextField.snp.right)
            make.height.equalTo(44)
            make.top.equalTo(passwordTextfield.snp.bottom).offset(44)
        }
        
        thirdLoginLabel.snp.makeConstraints { (make) in
            make.centerX.equalTo(view.snp.centerX)
            make.top.equalTo(registerButton.snp.bottom).offset(44)
        }
        
        qqButton.snp.makeConstraints { (make) in
            make.centerX.equalTo(view.snp.centerX)
            make.top.equalTo(thirdLoginLabel.snp.bottom).offset(17)
        }
        
        wechatButton.snp.makeConstraints { (make) in
            make.centerY.equalTo(qqButton.snp.centerY)
            make.left.equalTo(view.snp.left).offset(60)
        }
        
        weiboButton.snp.makeConstraints { (make) in
            make.centerY.equalTo(qqButton.snp.centerY)
            make.right.equalTo(view.snp.right).offset(-60)
        }

        

        
    }
    
    override func setupNavigationBar() {
        
    }
    
    override func bindViewModel() {
        
//        let accessId = cellNumberTextField.text ?? ""
//        let accessType = "FK"
//        let passwd = passwordTextfield.text ?? ""
//        viewModel!.actionForRegister().values.observeValues { (values) in
//            print(value)
//        }
        
        registerButton.reactive.pressed = CocoaAction(viewModel!.actionForRegister(), input: ("15001303511","FK","123456"))
        
//        registerButton.reactive.controlEvents(.touchUpInside).flatMap(.latest) { (button) -> SignalProducer<Any, NoError> in
//            return PKLoginServices.register(accessId: accessId, accessType: accessType, passwd: passwd)
//        }
        
        //键盘自动弹出
        self.reactive.trigger(for: #selector(UIViewController.viewDidAppear(_:))).observeValues { (_) in
            self.cellNumberTextField.becomeFirstResponder()
        }

    }


}
